package display;

import option.Data;
import option.Sweeper;
import paihang.Rank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MenuPanel extends JPanel {
    public static OptionFrame optionFrame = new OptionFrame();
    private static final AboutUs aboutUs = new AboutUs();
    private static final Help help = new Help();

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(Toolkit.getDefaultToolkit().getImage("src/image/MAEU.jpg"), 0, 0, 90 + 35 * Data.x, 70, this);
    }

    public MenuPanel() {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.setPreferredSize(new Dimension(90, 40));

        JMenuBar menubar = new JMenuBar();
        JMenu menu1 = new JMenu("游戏");
        JMenu menu2 = new JMenu("帮助");
        JMenuItem newgame = new JMenuItem("新游戏");
        JMenuItem options = new JMenuItem("难度设置");
        JMenuItem ranking=new JMenuItem("排行榜");
        JMenuItem exit = new JMenuItem("退出");
        JMenuItem showhelp = new JMenuItem("查看帮助");
        JMenuItem about = new JMenuItem("关于");

        //添加退出监听器
        exit.addActionListener(e -> System.exit(0));
        //添加新游戏监听器
        newgame.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Sweeper.newGame();
            }
        });
        //添加难度选择监听器
        options.addActionListener(new AbstractAction() {
            /**
             *
             * 打开难度调节界面
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                optionFrame.setVisible(true);
            }
        });
        //添加关于扫雷监听器
        about.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                aboutUs.setVisible(true);
            }
        });
        //添加帮助监听器
        showhelp.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                help.setVisible(true);
            }
        });
        //添加排行榜监听器
        ranking.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RankFrame rankFrame=new RankFrame();
                rankFrame.run();
                rankFrame.setVisible(true);
            }
        });

        menubar.setBorder(null);
        menubar.setOpaque(false);
        /*设置背景*/
        newgame.setBackground(new Color(201,226,252));
        options.setBackground(new Color(201,226,252));
        exit.setBackground(new Color(201,226,252));
        showhelp.setBackground(new Color(201,226,252));
        about.setBackground(new Color(201,226,252));
        ranking.setBackground(new Color(201,226,252));
        /*设置字体*/
        JLabel l=new JLabel();
        Font font1 = new Font("微软雅黑", Font.PLAIN+ Font.BOLD, 20);
        Font font2 = new Font("微软雅黑", Font.PLAIN, 18);
        l.setFont(font1);
        l.setFont(font2);
        l.setForeground(Color.white);
        menu1.setFont(font1);
        menu2.setFont(font1);
        newgame.setFont(font2);
        options.setFont(font2);
        ranking.setFont(font2);
        exit.setFont(font2);
        showhelp.setFont(font2);
        about.setFont(font2);

        menu1.add(newgame);
        menu1.add(options);
        menu1.add(ranking);
        menu1.add(new JSeparator());
        menu1.add(exit);
        menu2.add(showhelp);
        menu2.add(about);
        menubar.add(menu1);
        menubar.add(menu2);
        this.add(menubar);


    }
}
